package com.ywgt.response;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.HashMap;
import java.util.Map;

@Data
@Accessors
public class Result {

    @ApiModelProperty("返回状态码")
    private Integer code;
    @ApiModelProperty(value = "返回信息")
    private String message;
    @ApiModelProperty(value = "是否成功")
    private Boolean flag;
    @ApiModelProperty(value = "响应数据")
    private Map<String, Object> data=new HashMap<>();

    /**
     * 构造方法私有化，使用静态方法
     * 达到保护属性的作用
     */
    private Result(){

    }

    /**
     * 链式编程
     * @return
     */
    public static Result ok(){
        Result result=new Result();
        result.setFlag(true);
        result.setCode(ResultCode.SUCCESS.getResultCode());
        result.setMessage(ResultCode.SUCCESS.getResultMessage());
        return result;
    }

    public static Result error(){
        Result result=new Result();
        result.setFlag(false);
        result.setCode(ResultCode.ERROR.getResultCode());
        result.setMessage(ResultCode.ERROR.getResultMessage());
        return result;
    }

    /**
     * 自定义返回成功与否
     * @param success
     * @return
     */
    public Result success(Boolean success){
        this.setFlag(success);
        return this;
    }

    /**
     *自定义返回消息
     */
    public Result message(String message){
        this.setMessage(message);
        return this;
    }
    /**
     *自定义返回状态码
     */
    public Result code(Integer code){
        this.setCode(code);
        return this;
    }
    /**
     *自定义返回数据
     */
    public Result data(String key,Object value){
        this.data.put(key,value);
        return this;
    }

    public Result data(Map<String, Object> data){
        this.setData(data);
        return this;
    }
}
